﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.product
{
    public partial class productclassnav : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("001001005","对不起，您没有该权限");
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormInt("id") > 0)
                {
                    string types = Request.Form["Option"].Trim();
                    int id = ChangeHope.WebPage.PageRequest.GetFormInt("id");
                    if (types == "del")
                    {
                        if (YXShop.Common.PromptInfo.Message("001001003") != "ok")
                        {
                            del(id);
                        }
                        else
                        {
                            Response.Write("no");
                        }
                    }
                    else if (types == "sort")
                    {
                        if (YXShop.Common.PromptInfo.Message("001001004") != "ok")
                        {
                        int Num = ChangeHope.WebPage.PageRequest.GetFormInt("sort");
                        sort(id, Num);
                         }
                        else
                        {
                            Response.Write("no");
                        }
                    }
                    Response.End();
                    return;
                }
                this.Literal1.Text = GetList();
                if (ChangeHope.WebPage.PageRequest.GetInt("id") > 0)
                {
                    BandInfo(ChangeHope.WebPage.PageRequest.GetInt("id"));
                }
                InitWebControl();
            }
        }
        #region 列表信息
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        protected string GetList()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Product.ProductClassNav data = new YXShop.BLL.Product.ProductClassNav();
            ChangeHope.DataBase.DataByPage dataPage = data.GetList();
            //第一步先添加表头
            table.AddHeadCol("5%", "序号");
            table.AddHeadCol("25%", "名称");
            table.AddHeadCol("35%", "所属分类");
            table.AddHeadCol("10%", "排序");
            table.AddHeadCol("20%", "操作");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol(No);
                    table.AddCol("" + dataPage.DataReader["name"].ToString() + "");
                    table.AddCol(ProductClassName(dataPage.DataReader["cid"].ToString()));
                    table.AddCol(string.Format("<input id='sort{0}' value='{1}' size='5' onblur='Sort({0},{1})'/>", dataPage.DataReader["nid"].ToString(), dataPage.DataReader["sort"].ToString()));
                    table.AddCol(string.Format("<a href=productclassnav.aspx?id={0}>编辑</a> <a href='#' onclick='Del({0})'>删除</a>", dataPage.DataReader["nid"].ToString()));

                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        private void del(int id)
        {
            YXShop.BLL.Product.ProductClassNav bll = new YXShop.BLL.Product.ProductClassNav();
            YXShop.Model.Product.ProductClassNav model = bll.GetModelID(id);
            if (model != null)
            {
                bll.Delete(id);
            }
            Response.Write("ok");
        }

        private void sort(int id, int Sort)
        {
            YXShop.BLL.Product.ProductClassNav bll = new YXShop.BLL.Product.ProductClassNav();
            bll.Amend(id, "sort", Sort);
            Response.Write("ok");
        }
        #endregion
        protected void Save_Click(object sender, EventArgs e)
        {
            Save();
            this.Literal1.Text = GetList();
        }
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            this.txtProductClass.Attributes.Add("readonly", "readonly");
            this.txtProductClass.Attributes.Add("onclick", "selectFile('Productclass',new Array(" + this.hfcid.ClientID + "," + this.txtProductClass.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            ChangeHope.WebPage.WebControl.Validate(this.txtProductClass, "选择商品分类的名称,您可以在前台导航条信息。", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtSort, "输入数字将作为显示的排列顺序", "isint", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
            this.txtProductClass.Attributes.Add("readonly", "readonly");
        }
        protected void BandInfo(int id)
        {
            YXShop.BLL.Product.ProductClassNav bll = new YXShop.BLL.Product.ProductClassNav();
            YXShop.Model.Product.ProductClassNav model = bll.GetModelID(id);
            hfcid.Value = model.CID;
            this.txtProductClass.Text = ProductClassName(model.CID.ToString());
            this.txtName.Text = model.Name;
            this.txtSort.Text = model.Sort.ToString();
            ViewState["ID"] = model.NID;
        }
        /// <summary>
        /// 获取分类名称
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        protected string ProductClassName(string strId)
        {
            string reStr = string.Empty;
            if (!string.IsNullOrEmpty(strId))
            {
                YXShop.BLL.Product.Productclass dll = new YXShop.BLL.Product.Productclass();
                DataTable dt = dll.GetMoreThanClassName(strId);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(reStr))
                    {
                        reStr = reStr + "," + dt.Rows[i]["name"].ToString();
                    }
                    else
                    {
                        reStr = dt.Rows[i]["name"].ToString();
                    }
                }
            }
            return reStr;
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        protected void Save()
        {
            YXShop.BLL.Product.ProductClassNav bll = new YXShop.BLL.Product.ProductClassNav();
            YXShop.Model.Product.ProductClassNav model = new YXShop.Model.Product.ProductClassNav();
            model.CID = !string.IsNullOrEmpty(hfcid.Value.Trim()) ? hfcid.Value.Trim() : "0";
            if (txtName.Text.Trim() != string.Empty)
            {
                model.Name =this.txtName.Text.Trim();
            }
            else
            {
                model.Name = this.txtProductClass.Text.Trim();
            }
            model.Sort = ChangeHope.Common.StringHelper.StringToInt(txtSort.Text.Trim());
            if (ViewState["ID"] != null)
            {
                model.NID = int.Parse(ViewState["ID"].ToString());
                bll.Update(model);
                this.ltlMsg.Text = "操作成功，已保存该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
            else
            {
                bll.Add(model);
                
                this.ltlMsg.Text = "操作成功，已保存该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
                this.txtProductClass.Text = string.Empty;
            }
        }
    }
}
